<template>
  <div class="Repair_Application" v-if="result">
    <div class="Repair_Application_top">
      <span>温馨提示：如您已提交申请，请耐心等待评审结果，勿重复提交！</span>

      <button @click="change" class="Repair_Application_Refresh">
        报修历史
      </button>
    </div>

    <div class="Repair_Application_bottom">
      <el-form
        :model="form"
        :label-position="labelPosition"
        label-width="100px"
      >
        <el-form-item label="申请人:">
          <el-input :disabled="true" v-model="form.pricipalName"></el-input>
        </el-form-item>
        <el-form-item label="团队名称:">
          <el-input :disabled="true" v-model="form.teamName"></el-input>
        </el-form-item>

        <el-form-item label="维修物品:" id="Item_selection">
          <el-select
            v-model="form.typeId"
            clearable
            placeholder="请选择维修类别"
            @change="select1"
          >
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
          <span>—————</span>
          <el-select
            :disabled="disabled"
            v-model="form.repairsItemsId"
            clearable
            placeholder="请选择物品名称"
          >
            <el-option
              v-for="item in options_item"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="维修描述:" id="Maintenance_description">
          <el-input
            placeholder="维修描述"
            v-model="form.description"
            type="textarea"
          ></el-input>
        </el-form-item>

        <div class="Repair_Application_Submit">
          <el-button @click="Submit_form">提交</el-button>
          <el-button @click="empty_form">取消</el-button>
        </div>
      </el-form>
    </div>
  </div>
  <div v-else class="Repair_Application">
    <div class="Bread_crumbs">
      <span @click="result = true">报修申请&nbsp;</span>/
      <span>报修历史</span>    
    </div>
    <div class="Warranty_history_bottom">
      <el-table :data="tableData" height="900" :row-style="{ height: '50px' }">
        <el-table-column prop="typeName" align="center" label="维修类型">
        </el-table-column>
        <el-table-column align="center" prop="itemsName" label="维修物品">
        </el-table-column>
        <el-table-column align="center" prop="customerName" label="提交人">
        </el-table-column>
        <el-table-column align="center" prop="createTime" label="提交日期">
        </el-table-column>
        <el-table-column align="center" prop="endTime" label="维修时间">
        </el-table-column>
        <el-table-column align="center" label="维修状态" prop="repairsStatus">
          <template slot-scope="scope">
            <span v-show="scope.row.repairsStatus == 0">尚未指派</span>
            <span v-show="scope.row.repairsStatus == 1">已指派</span>
            <span v-show="scope.row.repairsStatus == 2">维修中</span>
            <span v-show="scope.row.repairsStatus == 3">维修完成</span>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        background
        class="Repair_history_page"
        layout="prev, pager, next"
        :page-count="page_count"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  props: {},
  components: {},
  data() {
    return {
      labelPosition: "right",
      disabled: true,
      result: true,
      page: 1,
      page_count: 1,
      pageSize: 5,
      height: "",
      form: {
        pricipalName: " ",
        teamName: "",
        typeId: "",
        repairsItemsId: "",
        description: "",
      },
      options: [],
      options_item: [],
      tableData: [],
    };
  },
  methods: {
    async select1(value) {
      this.disabled = false;
      this.form.repairsItemsId = "";

      const { data: res } = await this.$http({
        headers: { "Content-Type": "application/json;charset=UTF-8" },
        method: "get",
        url: "/api/usual/getRepairsItems?typeId=" + value,
      });

      this.options_item = res.msg;
      console.log(this.options_item);
    },

    change() {
      this.result = false;
    },

    async Submit_form() {
      console.log(this.form);
      const self = this;
      const { data: res } = await this.$http({
        headers: { "Content-Type": "application/json;charset=UTF-8" },
        method: "post",
        url: "/api/usual/addRepairs",
        data: {
          typeId: self.form.typeId,
          repairsItemsId: self.form.repairsItemsId,
          description: self.form.description,
        },
      });

      if (res.code != 200) {
        this.$message.error("提交失败");
      }
      this.$message.success(res.msg);
      this.form.typeId = "";
      this.form.repairsItemsId = "";
      this.form.description = "";
    },
    // Refresh() {
    //   location.reload();
    // },
    empty_form() {
      this.form.typeId = "";
      this.form.repairsItemsId = "";
      this.form.description = "";

      // console.log(this.form);
    },

    keyDown(e) {
      //如果是回车则执行登录方法
      if (e.keyCode == 13) {
        this.Submit_form();
        return;
      }
    },
    handleCommand(command) {
      this.form.Roadshow_batch = command;
      // this.$message.success(command);
    },
    //确认按钮
    handleEdit(index, row) {
      console.log(index, row);
      row.repaisStatus = true;
    },
    //监听页码值改变的事件
    handleCurrentChange(newSize) {
      this.page = newSize;
      this.get_Repair_history();
    },

    async get_Repair_history() {
      const self = this;
      const { data: res } = await this.$http({
        headers: { "Content-Type": "application/json;charset=UTF-8" },
        method: "get",
        url: "/api/usual/getRepairs",
        params: {
          page: self.page,
          pageSize: self.pageSize,
        },
      });

      this.page_count = res.msg.pages;
      this.tableData = res.msg.records;

      console.log(this.page_count);
      // console.log( this.tableData);
    },
    getHeight() {
      // 获取浏览器高度，减去顶部导航栏的值70（可动态获取）,再减去head-DIV高度值80
      this.height = window.innerHeight;
      this.pageSize = parseInt((this.height * 0.61) / 45);

      this.get_Repair_history();
    },
  },
  mounted() {
    //绑定事件
    window.addEventListener("keydown", this.keyDown);
  },

  async created() {
        this.form.pricipalName = window.localStorage.getItem("name");
    this.form.teamName = window.sessionStorage.getItem("TeamName");

    const { data: res } = await this.$http({
      headers: { "Content-Type": "application/json;charset=UTF-8" },
      method: "get",
      url: "/api/usual/getRepairsTypes",
    });

    this.options = res.msg;
    // console.log(res);
    this.get_Repair_history();
    window.addEventListener("resize", this.getHeight); // 注册监听器
    this.getHeight();
  },
  destroyed() {
    window.removeEventListener("keydown", this.keyDown);
    window.removeEventListener("resize", this.getHeight);
  },
};
</script>

<style lang="less">
.Repair_Application {
  // background-color: rgb(167, 19, 19);
  height: 98%;
  width: 100%;
  margin: 10px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  text-overflow: ellipsis; /*超出省略*/
}
.Repair_Application_top {
  display: flex;
  // width: 100%;
  height: 80px;
  // background-color: rgb(212, 24, 24);
  justify-content: space-around;

  span {
    border-radius: 10px;
    width: 50%;
    background-color: rgb(229, 240, 255);
    margin: auto;
    text-align: center;
    height: 50px;
    line-height: 50px;
  }
}
.Repair_Application_Refresh {
  transform: translateX(-50%);
  height: 40px;
  width: 100px;
  display: flex;
  // margin-right: 100px;
  justify-content: space-around;
  line-height: 40px;
  text-align: center;
  font-size: 16px;
  background-color: #fff;
  border: 1px solid rgb(215, 215, 215);
  border-radius: 15px;
  cursor: pointer;
  margin: auto 0px;
  // i {
  //   height: 40px;
  //   line-height: 40px;
  //   font-size: 20px;
  // }
}
.Repair_Application_bottom {
  margin: 15px;
  border-radius: 10px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.12), 0 0 10px rgba(0, 0, 0, 0.04);
  height: 90%;
  width: 97%;
  display: flex;
  flex-direction: column;
  .el-form {
    display: flex;
    flex-direction: column;
    height: 90%;
    width: auto;

    margin: auto;
    margin-top: 30px;
    .el-form-item {
      display: flex;
      flex-direction: row;
      height: 10%;
      .el-form-item__label {
        font-size: 20px;
        height: 60px;
        line-height: 60px;
        font-weight: bold;
        width: 150px !important;
        text-align: center;

        float: right;
      }
      .el-input {
        width: 700px;

        .el-input__inner {
          height: 60px;
          font-size: 19px;
        }
      }
    }
  }
}

.Repair_Application_Submit {
  height: 80px;
  width: auto;
  display: flex;
  margin: 0px auto;
  // margin-top: 50px;
  button {
    width: 200px;
    border: none;
  }
  button:nth-child(1) {
    background-color: rgb(0, 110, 255);
    color: #fff;
    font-size: 20px;
    font-weight: bolder;
  }
  button:nth-child(2) {
    background-color: rgb(242, 242, 242);
    margin-left: 150px;
    font-size: 20px;
  }
}

#Item_selection {
  display: flex;
  flex-direction: row;
  .el-input--suffix {
    width: 311px;
  }
  .el-form-item__content {
    display: flex;
    justify-content: space-between;
    span {
      height: 60px;
      line-height: 60px;
      display: flex;
    }
  }
}

#Maintenance_description {
  display: flex;
  flex-direction: row;
  height: 200px;
  .el-textarea__inner {
    display: flex;
    width: 700px;
    font-size: 25px;
    height: 200px;
  }
}

.Bread_crumbs {
  margin: auto 0px;
  span {
    cursor: pointer;
  }

  span:nth-child(1) {
    color: rgb(127, 127, 127);
  }
  span:nth-child(2) {
    color: rgb(0, 110, 255);
  }
  span:hover {
    color: rgb(0, 110, 255);
  }
}

//报修历史
.Warranty_history_bottom {
  // background-color: rgb(224, 21, 21);

  margin-top: 10px;
  height: 93%;
  width: 98%;
  display: flex;
  box-shadow: 0 2px 14px rgba(0, 0, 0, 0.12), 0 0 10px rgba(0, 0, 0, 0.04);
  border-radius: 10px;
  border: 1px solid rgb(222, 222, 222);
  flex-direction: column;
  .el-table {
    margin: 20px;
    width: 98%;
    font-size: 17px;
  }

  .el-table__body-wrapper::-webkit-scrollbar-thumb {
    border-radius: 3px;
    background: #fff;
  }
  .el-table__body-wrapper::-webkit-scrollbar {
    width: 6px;
  }
  .el-table__body-wrapper:hover::-webkit-scrollbar-thumb {
    background: rgba(0, 0, 0, 0.2);
  }
}

.Repair_history_page {
  height: 3%;
  width: 100%;
  display: flex;
  justify-content: end;
  margin: 0px 20px 10px 0px;
}
</style>